package com.ybkj.o2o.sales.service.impl;

import com.ybkj.o2o.sales.common.CouponNumberConfig;
import com.ybkj.o2o.sales.entity.Activity;
import com.ybkj.o2o.sales.entity.Activity.ActivityEvent;
import com.ybkj.o2o.sales.entity.Coupon;
import com.ybkj.o2o.sales.entity.CouponRule;
import com.ybkj.o2o.sales.mapper.ActivityCouponRuleMapper;
import com.ybkj.o2o.sales.mapper.ActivityMapper;
import com.ybkj.o2o.sales.mapper.CouponMapper;
import com.ybkj.o2o.sales.service.ActivityService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DefaultActivityService implements ActivityService {

  @Autowired
  private ActivityMapper activityMapper;
  @Autowired
  private ActivityCouponRuleMapper activityCouponRuleMapper;
  @Autowired
  private CouponMapper couponMapper;

  /**
   * 事务回滚.
   */
  @Transactional(rollbackFor = Exception.class)
  public Long insert(Activity activity) {

    return activityMapper.insertRecord(activity);

  }

  /**
   * 事务回滚.
   */
  @Transactional(rollbackFor = Exception.class)
  public void delete(Long id, String appKey) {

    activityMapper.deleteByPrimaryKey(id, appKey);
    activityCouponRuleMapper.deleteByActivity(id);

  }

  /**
   * 事务回滚.
   */
  @Transactional(rollbackFor = Exception.class)
  public int update(Activity activity) {

    return activityMapper.updateByPrimaryKey(activity);

  }

  /**
   * 事务回滚.
   */
  @Transactional(rollbackFor = Exception.class)
  public void stop(Long id, String appKey) {

    activityMapper.stopByPrimaryKey(id, appKey);

  }

  @Override
  public Activity findOne(Long id, String appKey) {

    return activityMapper.selectByPrimaryKey(id, appKey);

  }

  @Override
  public List<Activity> selectActivities(Long companyId, String activityEvent, String activityType,
      Integer offset, Integer size, String appKey) {

    return activityMapper.selectActivities(companyId, activityEvent, activityType, offset, size,
        appKey);

  }

  @Override
  public List<Activity> selectAllActivities(Long companyId, Integer offset, Integer size,
      String appKey) {

    return activityMapper.selectAllActivities(companyId, offset, size, appKey);

  }

  @Override
  public Long countActivities(Long companyId, String activityEvent, String activityType,
      String appKey) {
    return activityMapper.countActivities(companyId, activityEvent, activityType, appKey);

  }

  @Override
  public Long countAllActivities(Long companyId, String appKey) {
    return activityMapper.countAllActivities(companyId, appKey);

  }

  @Override
  public List<Activity> listValidByEvent(ActivityEvent activityEvent, String appKey) {

    return activityMapper.selectValidByEvent(activityEvent, appKey);

  }

  @Override
  public List<Activity> activityList(ActivityEvent activityEvent, Long[] linesId, String appKey) {
    List<Long> idList = new ArrayList<>();
    for(int i = 0;i < linesId.length;i++){
      idList.add(linesId[i]);
    }
    return activityMapper.activityList(activityEvent, idList, appKey);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void phoneCoupon(String phone, CouponRule couponRule, Integer amount, String appKey) {

    for (int i = 0; i < amount; i++) {
      Coupon coupon = new Coupon();

      Long number = couponMapper.getRunningNumber(couponRule.getId(), appKey);
      if (null == number) {
        number = couponMapper.getTotalCountNumber(couponRule.getId(), appKey);
      }
      if (null == number) {
        number = 0L;
      }

      coupon.setRunCode(number + 1);
      coupon.setCode(couponRule.getCode() + CouponNumberConfig.padZero(number + 1));
      coupon.setCouponRuleId(couponRule.getId());
      coupon.setPhone(phone);
      coupon.setStatus(Coupon.COUPON_STATUS_NOTUSE);// 未用（已绑定）
      couponMapper.insertRecord(coupon);
    }


  }

  @Override
  public List<Activity> findOneFreeActivity(Long companyId, ActivityEvent activityEvent,
      String appKey) {

    return activityMapper.selectOneFreeActivity(companyId, activityEvent, appKey);
  }



}
